package ch.bfh.aufs.prologparser.view.subpanels;

import java.awt.BorderLayout;
import java.awt.ScrollPane;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;

import ch.bfh.aufs.prologparser.controller.PPController;

public class DisplayPanel extends JPanel implements Observer{
    
    private static final long serialVersionUID = 1L;
    private ArrayList<String> tokens;
    private ScrollPane scrollPane;
    private JList<String> tokenList;
    private DefaultListModel<String> listModel;
    private FileChoosePanel choosePanel;
    
    public DisplayPanel(PPController controller, FileChoosePanel choosePanel){
        setLayout(new BorderLayout());
        this.choosePanel = choosePanel;
        this.scrollPane = new ScrollPane();
        this.listModel = new DefaultListModel<String>();
        this.tokenList = new JList<>(listModel);
        this.scrollPane.add(tokenList);
        this.scrollPane.setWheelScrollingEnabled(true);
        this.add(scrollPane,BorderLayout.CENTER);

    }

    @SuppressWarnings("unchecked")
    @Override
    public void update(Observable o, Object arg) {
        listModel.removeAllElements();
        tokens = (ArrayList<String>) arg;
        for (String token : tokens) {
            listModel.addElement(token);
        }
        
        if (listModel.size()>0) {
            choosePanel.activateOutputbutton();
        }
    }

}
